import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_train.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/14.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class TrainCourseItemWidget extends StatefulWidget {
  final CourseAndTeacherBean cubean;
  final int index;

  const TrainCourseItemWidget({
    Key key,
    this.cubean,
    this.index,
  }) : super(key: key);

  @override
  _TrainCourseItemWidgetState createState() => _TrainCourseItemWidgetState();
}

class _TrainCourseItemWidgetState extends State<TrainCourseItemWidget>
    with SingleTickerProviderStateMixin {
  bool _isOpen = false;
  AnimationController _animationController;
  Animation<Offset> _animation;

  @override
  void initState() {
    super.initState();
    _animationController = new AnimationController(
        vsync: this,
        duration: Duration(microseconds: 400),
        lowerBound: 0.0,
        upperBound: 0.25);
    _animation = new Tween(begin: Offset(0, -0.5), end: Offset(0.0, 0.0))
        .animate(_animationController);
  }

  List<CourseBean> _list = [];

  @override
  void dispose() {
    if (_cancelToken != null && !_cancelToken.isCancelled) {
      _cancelToken.cancel();
    }
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        if (!_isOpen) {
          _isOpen = true;
          if (_list.length == 0) {
            getCourseLevelList();
          }
          _animationController.reset();
          _animationController.forward();
        } else {
          _isOpen = false;
          _animationController.reverse();
        }
        setState(() {});
      },
      child: Container(
        margin: EdgeInsets.only(top: 6),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Row(
              children: [
                ClipOval(
                  child: Container(
                    alignment: Alignment.center,
                    width: 14,
                    height: 14,
                    child: Text(
                      "${widget.index + 1}",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 10,
                      ),
                    ),
                    decoration: BoxDecoration(
                        gradient: LinearGradient(colors: [
                      Colors.blueAccent,
                      Colors.lightBlueAccent,
                      Colors.blue,
                    ])),
                  ),
                ),
                SizedBox(
                  width: 8,
                ),
                Text("${widget.cubean.userEntity.reamName}"),
                SizedBox(
                  width: 8,
                ),
                Expanded(
                  child: Text("《${widget.cubean.teacherContent}》"),
                ),
                RotationTransition(
                  turns: _animationController,
                  child: Icon(Icons.arrow_right),
                )
              ],
            ),
            Container(
              child: buildCourseLevel(),
              padding: EdgeInsets.only(left: 22),
            ),
          ],
        ),
      ),
    );
  }

  buildCourseLevel() {
    if (!_isOpen) {
      return Container();
    }
    if (_isLoading) {
      return LoadingWidget();
    }
    if (_list.length == 0) {
      return Text("暂无课程小节");
    }
    return ListView.builder(
      shrinkWrap: true,
      physics: BouncingScrollPhysics(),
      itemCount: _list.length,
      itemBuilder: (BuildContext context, int index) {
        CourseBean courseBean = _list[index];
        return Container(
          child: Text(
            "${widget.index + 1}-${index + 1}  ${courseBean.courseName}",
            style: TextStyle(fontSize: 12),
          ),
        );
      },
    );
  }

  //获取课程小节

  CancelToken _cancelToken;
  bool _isLoading = true;

  Future<bool> getCourseLevelList() async {
    if (_cancelToken != null && !_cancelToken.isCancelled) {
      _cancelToken.cancel();
    }
    _cancelToken = new CancelToken();
    //答题数据
    ResponseInfo statisRresponseInfo = await DioUtils.instance.getRequest(
      url:
          HttpHelper.courseLevelUrl + "/${widget.cubean.id}",
      cancelTag: _cancelToken,
    );
    if (statisRresponseInfo.success) {
      List list = statisRresponseInfo.data;
      if (list != null) {
        _list = [];
        list.forEach((element) {
          _list.add(CourseBean.fromJson(element));
        });
      }
    }
    _isLoading = false;
    if (mounted) {
      setState(() {});
    }

    return Future.value(true);
  }
}
